package sat.component;

import java.io.File;

import org.mt4j.MTApplication;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.shapes.MTRoundRectangle;
import org.mt4j.components.visibleComponents.widgets.MTList;
import org.mt4j.components.visibleComponents.widgets.MTListCell;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.rotateProcessor.RotateProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.scaleProcessor.ScaleProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;

import processing.core.PImage;
import sat.component.ClassComponent.entityType;


public class ClassPopupItem 
//extends MTRectangle{ 
extends MTListCell {

	private static final float width = UMLPopUp.widthPreferred;
	private static final float height = 38;

	private static final float buttonRadius = 10;
	private static final float cellWidth = width - 5*buttonRadius;
	private static final float cellHeight = 30;

	private static final float horizontalOffset = 3;
	private static final float verticalOffset = 5;

	private MTApplication app;
	private MTList list;
	private ClassComponentPopUp ccPopUp;

	MTRoundRectangle addButton;
	MTRoundRectangle deleteButton;

	KeyboardTextField elementField;

	public void refreshView(){
		int nbElement = list.getChildByIndex(0).getChildCount();
		MTList attList = ccPopUp.getAttributeList();
		MTList operList = ccPopUp.getOperationList();
		
		if(list == attList){
			attList.setHeightLocal(
					(float)(nbElement > 3 ? 
							nbElement*(height+2) : ClassComponentPopUp.listWidthPreferred));
			operList.setPositionRelativeToParent(
					new Vector3D(ClassComponentPopUp.widthPreferred/2,
							ClassComponentPopUp.nameFieldHeight/2+attList.getHeightXY(TransformSpace.RELATIVE_TO_PARENT)
							+operList.getHeightXY(TransformSpace.RELATIVE_TO_PARENT)/2+15)
					);
		}
		else{
			operList.setHeightLocal(
					(float)(nbElement > 3 ? nbElement*(height+2) : ClassComponentPopUp.listWidthPreferred));
		}
		ccPopUp.setHeightLocal(ClassComponentPopUp.nameFieldHeight + 
				(ccPopUp.geteType() == entityType.CLASS ? attList.getHeightXY(TransformSpace.LOCAL) : 0) +
				operList.getHeightXY(TransformSpace.LOCAL));
		ccPopUp.refreshBottomButtonPositionY();
	}

	public void addEmptyItem(){
		addButton.setVisible(false);
		elementField.setVisible(true);
		deleteButton.setVisible(true);
		list.addListElement(new ClassPopupItem(app,list,ccPopUp));
		refreshView();

	}
	public void deleteItem(){
		list.removeListElement(this);
		refreshView();
	}

	public String toString(){
		return elementField.getText();
	}

	public ClassPopupItem(MTApplication mtApplication,MTList l,ClassComponentPopUp ppp) {
		super(mtApplication, width, height);
		// TODO Auto-generated constructor stub

		list = l;
		app = mtApplication;
		ccPopUp = ppp;
		this.setNoStroke(true);
		this.setComposite(false);
		this.removeAllGestureEventListeners();
		this.setFillColor(new MTColor(122, 142, 117, 255));

		addButton = new MTRoundRectangle(mtApplication, 0, 0, 0, buttonRadius*2, buttonRadius*2 , buttonRadius, buttonRadius);
		addButton.setPositionRelativeToParent(new Vector3D(buttonRadius+horizontalOffset,cellHeight/2));
		//addButton.setStrokeColor(MTColor.BLACK);
		addButton.setNoStroke(true);

		String imagePath = System.getProperty("user.dir") + File.separator + "icons" + File.separator;

		PImage addIcon = mtApplication.loadImage(imagePath + "add.png");
		addButton.setTexture(addIcon);

		elementField = new KeyboardTextField(mtApplication, cellWidth,cellHeight,KeyboardTextField.type.classes,ccPopUp,null);
		elementField.setPositionRelativeToParent(new Vector3D(horizontalOffset+cellWidth/2,cellHeight/2+verticalOffset));
		elementField.setStrokeColor(new MTColor(164, 117, 91, 255));
		elementField.setStrokeWeight(3);
		elementField.setFillColor(new MTColor(215, 195, 168, 255));
		elementField.setVisible(false);
		elementField.removeAllGestureEventListeners(DragProcessor.class);
		elementField.removeAllGestureEventListeners(RotateProcessor.class);
		elementField.removeAllGestureEventListeners(ScaleProcessor.class);

		deleteButton = new MTRoundRectangle(mtApplication, 0, 0, 0, buttonRadius*2, buttonRadius*2, buttonRadius, buttonRadius);
		deleteButton.setPositionRelativeToParent(new Vector3D(width-buttonRadius-horizontalOffset,height/2));
		deleteButton.setStrokeColor(MTColor.BLACK);
		deleteButton.setVisible(false);

		PImage delIcon = mtApplication.loadImage(imagePath + "quit.png");
		deleteButton.setTexture(delIcon);
		deleteButton.setNoStroke(true);

		this.addChild(addButton);
		this.addChild(elementField);
		this.addChild(deleteButton);

		// Listener:
		// gesture for pop-up
		addButton.removeAllGestureEventListeners();
		addButton.registerInputProcessor(new TapProcessor(mtApplication));
		addButton.addGestureListener(TapProcessor.class, elementField.getKeyboardListener()); // add keyboard when push the add button
		addButton.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			@Override
			public boolean processGestureEvent(MTGestureEvent ge) {
				// TODO Auto-generated method stub
				TapEvent te = (TapEvent)ge;
				switch (te.getTapID()) {
				case TapEvent.TAPPED:
					System.out.println("add tapped");
					addEmptyItem();
					break;
				default:
					break;
				}
				return false;
			}
		});

		deleteButton.removeAllGestureEventListeners();
		deleteButton.registerInputProcessor(new TapProcessor(mtApplication));
		deleteButton.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			@Override
			public boolean processGestureEvent(MTGestureEvent ge) {
				// TODO Auto-generated method stub
				TapEvent te = (TapEvent)ge;
				switch (te.getTapID()) {
				case TapEvent.TAPPED:
					System.out.println("delete tapped");
					deleteItem();
					break;
				default:
					break;
				}
				return false;
			}
		});
	}


}
